package com.sunwayworld.basemodule.basedata.zksample.service.impl;

import com.sunwayworld.basemodule.basedata.zksample.bean.LimsZkSampleTemplateBean;
import com.sunwayworld.basemodule.basedata.zksample.service.LimsZkSampleTemplateService;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.basedata.zksample.dao.LimsZkSampleDao;
import com.sunwayworld.basemodule.basedata.zksample.bean.LimsZkSampleBean;
import com.sunwayworld.basemodule.basedata.zksample.service.LimsZkSampleService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.List;

@Repository
@GikamBean
public class LimsZkSampleServiceImpl implements LimsZkSampleService {

    @Autowired
    private LimsZkSampleDao limsZkSampleDao;

    @Autowired
    private LimsZkSampleTemplateService zkSampleTemplateService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsZkSampleDao getDao() {
        return limsZkSampleDao;
    }

    @Override
    @Transactional
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsZkSampleBean limsZkSample = jsonWrapper.parseUnique(LimsZkSampleBean.class);
        limsZkSample.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsZkSample);
        return limsZkSample.getId();
    }

    @Override
    @Transactional
    public void delete(RestJsonWrapperBean wrapper) {
        List<Long> zkSampleIds = wrapper.parseId(Long.class);
        if (zkSampleIds.isEmpty()) return;
        List<LimsZkSampleTemplateBean> zkSampleTemplateBeans = zkSampleTemplateService.selectListByFilter(SearchFilter.instance().match("zkSampleId", zkSampleIds).filter(MatchPattern.OR));
        getDao().deleteByIdList(zkSampleIds);
        if (zkSampleTemplateBeans.isEmpty()) zkSampleTemplateService.getDao().deleteBy(zkSampleTemplateBeans);
    }
}
